package com.jeckey.demo;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 超市类：封装定价和促销规则，提供总价计算方法
 */
public class Supermarket {
    // 满减规则：满100减10
    private static final BigDecimal DISCOUNT_THRESHOLD = new BigDecimal("100.0");
    private static final BigDecimal DISCOUNT_AMOUNT = new BigDecimal("10.0");

    /**
     * 场景1：只买苹果和草莓（无促销）
     * @param appleWeight 苹果斤数（≥0整数）
     * @param strawberryWeight 草莓斤数（≥0整数）
     * @return 总价
     */
    public BigDecimal calculateTotal1(int appleWeight, int strawberryWeight) {
        Apple apple = new Apple();
        Strawberry strawberry = new Strawberry(); // 无折扣
        return apple.getSellingPrice().multiply(new BigDecimal(appleWeight))
                .add(strawberry.getSellingPrice().multiply(new BigDecimal(strawberryWeight)));
    }

    /**
     * 场景2：买苹果、草莓、芒果（无促销）
     * @param appleWeight 苹果斤数（≥0整数）
     * @param strawberryWeight 草莓斤数（≥0整数）
     * @param mangoWeight 芒果斤数（≥0整数）
     * @return 总价
     */
    public BigDecimal calculateTotal2(int appleWeight, int strawberryWeight, int mangoWeight) {
        Apple apple = new Apple();
        Strawberry strawberry = new Strawberry(); // 无折扣
        Mango mango = new Mango();
        return apple.getSellingPrice().multiply(new BigDecimal(appleWeight))
                .add(strawberry.getSellingPrice().multiply(new BigDecimal(strawberryWeight)))
                .add(mango.getSellingPrice().multiply(new BigDecimal(mangoWeight)));
    }

    /**
     * 场景3：买三种水果 + 草莓8折（无满减）
     * @param appleWeight 苹果斤数（≥0整数）
     * @param strawberryWeight 草莓斤数（≥0整数）
     * @param mangoWeight 芒果斤数（≥0整数）
     * @return 总价
     */
    public BigDecimal calculateTotal3(int appleWeight, int strawberryWeight, int mangoWeight) {
        Apple apple = new Apple();
        Strawberry strawberry = new Strawberry();
        strawberry.setDiscount(new BigDecimal("0.8")); // 草莓8折
        Mango mango = new Mango();
        return apple.getSellingPrice().multiply(new BigDecimal(appleWeight))
                .add(strawberry.getSellingPrice().multiply(new BigDecimal(strawberryWeight)))
                .add(mango.getSellingPrice().multiply(new BigDecimal(mangoWeight)));
    }

    /**
     * 场景4：买三种水果 + 草莓8折 + 满100减10
     * @param appleWeight 苹果斤数（≥0整数）
     * @param strawberryWeight 草莓斤数（≥0整数）
     * @param mangoWeight 芒果斤数（≥0整数）
     * @return 最终总价
     */
    public BigDecimal calculateTotal4(int appleWeight, int strawberryWeight, int mangoWeight) {
        // 先计算打折后的总价（同场景3）
        BigDecimal totalBeforeDiscount = calculateTotal3(appleWeight, strawberryWeight, mangoWeight);
        // 满减规则：满100减10，只减一次
        if (totalBeforeDiscount.compareTo(DISCOUNT_THRESHOLD) >= 0) {
            return totalBeforeDiscount.subtract(DISCOUNT_AMOUNT);
        }
        return totalBeforeDiscount;
    }
}